<?php
/**
 * Deco service
 */
namespace App\Service;

use App\Models\EwDeco;

class DecoService  extends BaseService
{
	const DISPLAY_TRUE = 1;		//显示标识：是
	const DISPLAY_FALSE = 0;	//显示标识：否
	const DEL_TRUE = 1;			//删除标识：是
	const DEL_FALSE = 0;		//删除标识：否

	/**
	 * [getList 房源列表]
	 * @param  [type] $params [description]
	 * @return [type]         [description]
	 */
	public static function getList($params, $count = FALSE)
	{
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns('d.id,d.id AS go_url,d.three_url,d.is_self,d.title,IFNULL(d.tags,"") AS tags,d.thumb,d.pics,IFNULL(d.index_by,0) AS index_by,IFNULL(d.reco_by,0) AS reco_by,d.sources');
		$builder->addFrom("App\Models\EwDeco", 'd');

		$builder->andWhere("d.is_del = " . self::DEL_FALSE);

		//city_id
		if (isset($params->city_id) && $params->city_id)
			$builder->andWhere("d.city_id in ( 0 , 1 ,"  .  $params->city_id . " )");

		//tags 
		if (isset($params->tags) && $params->tags)
			$builder->andWhere("d.tags = ". $params->tags);

		if (isset($params->is_display))
			$builder->andWhere("d.is_display = ". $params->is_display);

		//title 标题
		if (isset($params->title) && $params->title)
			$builder->andwhere('d.title LIKE "%'.$params->title.'%"');

		//sources 来源
		if (isset($params->sources) && $params->sources)
			$builder->andwhere('d.sources LIKE "%'.$params->sources.'%"');

		//comp_name 公司名称
		if (isset($params->comp_name) && $params->comp_name)
			$builder->andwhere('d.comp_name LIKE "%'.$params->comp_name.'%"');

		//count 数量
		if ($count)
			return $builder->getQuery()->execute()->count();

		// $sql = " d.order_by DESC, d.create_time DESC, d.id DESC, d.update_time DESC";
		// $sql = "n.order_by DESC, n.create_time DESC, n.id DESC, n.update_time DESC";
		$sql = " d.index_by DESC, d.order_by DESC, d.create_time DESC, d.id DESC, d.update_time DESC";//默认获取 首页 - deco
		if (isset($params->is_reco) && $params->is_reco)//获取推荐 - banner - deco
			$sql = " d.reco_by DESC, d.order_by DESC, d.create_time DESC, d.id DESC, d.update_time DESC";
		if (isset($params->is_reco) && $params->is_reco)//获取推荐 - banner - deco  2017-04-06+ banner默认请求三条
			$params->page_num = 3;
		//order_by 排序
		if (isset($params->order_by) && $params->order_by)
			$sql = parent::orderBy($params->order_by, $table = "d");

		$builder->orderBy($sql);

		$builder->limit($params->page_num,($params->p-1)*$params->page_num);
		$result = $builder->getQuery();
		return $result->execute();		
	}
	
	/**
	 * [getSearch 房源搜索]
	 * @param  [type] $params [description]
	 * @return [type]         [description]
	 */
	public static function getSearch($params, $count = FALSE)
	{
		
		$builder = new \Phalcon\Mvc\Model\Query\Builder();
		$builder->columns('d.id,d.three_url as go_url,d.title,d.tags,d.cate_id,d.thumb,d.pics,d.comp_name,d.sources,d.visit,d.comment,d.is_display,d.order_by,d.create_time');
		$builder->addFrom("App\Models\EwDeco", 'd');

		$builder->andWhere("d.is_del = " . self::DEL_FALSE);

		//position_id 通过position_id 获取后台弹出列表  1 首页
		if (isset($params->position_id) && $params->position_id == 2) //图库banner
			$builder->andWhere("d.reco_by  != '2' AND d.index_by != '2' AND d.is_display = 1");
		if (isset($params->position_id) && $params->position_id == 1) //图库首页
			$builder->andWhere("d.reco_by  != '2' AND d.index_by != '2' AND d.is_display = 1");

		//reco_by
		if (isset($params->reco_by))//首页banner列表 reco_by = 2
			$builder->andWhere("d.reco_by = '" . $params->reco_by . "'");

		//index_by
		if (isset($params->index_by))//首页视频列表 index_by = 2
			$builder->andWhere("d.index_by = '" . $params->index_by . "'");

		if (isset($params->is_display))
			$builder->andWhere("d.is_display = ". $params->is_display);

		//title 标题
		if (isset($params->title) && $params->title)
			$builder->andwhere('d.title LIKE "%'.$params->title.'%"');

		//sources 来源
		if (isset($params->sources) && $params->sources)
			$builder->andwhere('d.sources LIKE "%'.$params->sources.'%"');

		//company 公司
		if (isset($params->company) && $params->company)
			$builder->andwhere('d.company LIKE "%'.$params->company.'%"');

		//count 数量
		if ($count)
			return $builder->getQuery()->execute()->count();

		$sql = " d.order_by DESC, d.create_time DESC, d.id DESC, d.update_time DESC";
		//order_by 排序
		if (isset($params->order_by))
			$sql = parent::orderBy($params->order_by, $table = "d");

		$builder->orderBy($sql);

		$builder->limit($params->page_num,($params->p-1)*$params->page_num);
		$result = $builder->getQuery();
		return $result->execute();	
	}

	/**
	 * [getTableName 获取数据表名称]
	 * @return [type] [description]
	 */
	public static function getTableName()
	{
		$model = new EwDeco;
		return $model->getSource();
	}

	/**
	 * [getTableId 获取数据表Id]
	 * @return [type] [description]
	 */
	public static function getTableId()
	{
		return ConfigsService::getTable(self::getTableName());
	}

	public static function getModel($id = 0)
	{
		return EwDeco::findFirst($id);
	}

	/**
	 * [getInfo 通过主键获取房源详情]
	 * @param  integer $id [description]
	 * @return [type]      [description]
	 */
	public static function getInfo($id = 0, $member_id = NULL)
	{	
		$info = EwDeco::findFirst($id);
		if (!$info)
			return NULL;
		$info = $info->toArray();

		$data = new \stdClass();
		$data->go_url  = "";
		$data->groupId = self::getTableId();
		$data->praise = (string)0;
		$data->is_praise = (string)-1;

		$arr = new \stdClass();
		$arr->table_id = self::getTableId();
		$arr->list_id  = $id;
		$arr->list_id  = $id;
		$arr->praise   = NULL;
		//获取点赞量
		$data->praise = (string)MemberPraiseService::sum($arr);
		//获取当前用户是否点赞
		if ($member_id){
			$arr->member_id = $member_id;
			$data->is_praise = (string)MemberPraiseService::juge($arr);
		}
		
		foreach ($info as $key => $value) {
			$data->$key = $value;
		}
		
		return $data;
	}

	/**
	 * [count 获取总量]
	 * @return [type] [description]
	 */
	public static function count()
	{
		return EwDeco::count();
	}

	/**
	 * [getStick 置顶]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function getStick($id)
	{
		if (isset($id) && $id)
			$raw = EwDeco::findFirst($id);

		if ($raw){
			$max = EwDeco::maximum(['column' => 'order_by']);
			$raw->order_by = $max + 1;
			return $raw->save();
		}
		return NULL;
	}

	/**
	 * [visit 浏览量]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function visit($id)
	{
		if (isset($id) && $id)
			$data = EwDeco::findFirst($id);

		if ($data){
			$data->visit = $data->visit + 1;
			return $data->save();
		}

		return NULL;
	}

	/**
	 * [display 上架]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function display($id)
	{
		if (isset($id) && $id)
			$data = EwDeco::findFirst($id);

		if ($data){
			$data->is_display = $data->is_display ? self::DISPLAY_FALSE : self::DISPLAY_TRUE;
			// if (!$data->save())
			// {
			// 	$msg =$data->getMessages();
			// 	var_dump($msg);die;
			// }
			return $data->save();
		}

		return NULL;
	}

	/**
	 * [delete 移除/恢复数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function delete($id)
	{
		if (isset($id) && $id)
			$data = EwDeco::findFirst($id);

		if ($data){
			$data->is_del = $data->is_del ? self::DEL_FALSE : self::DEL_TRUE;
			return $data->save();
		}

		return NULL;		
	}

	/**
	 * [delete 彻底删除数据]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		if (isset($id) && $id)
			$row = EwDeco::findFirst($id);

		if ($row)
			return $row->delete();

		return NULL;
	}

	/**
	 * [save 保存]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public static function save($data)
	{
		$data->pics = json_encode($data->pics);
		$row = new EwDeco;

		if (isset($data->id) && $data->id)
			$row = EwDeco::findFirst($data->id);

		//置顶
		if (isset($data->is_stick) && $data->is_stick){
			$max = EwDeco::maximum(['column' => 'order_by']);
			$data->order_by = $max + 1;
		}

		// if (!$row->save((array)$data))
		// {
		// 	$msg =$row->getMessages();
		// 	var_dump($msg);die;
		// }
		return $row->save((array)$data);
	}

	/**
	 * 2017-3-28 +
	 * [batchReco 批量 推荐/批量删除 到 推荐列表  首页列表]
	 * @param  [type]  $id_arr [操作的id 数组形式]
	 * @param  integer $action [2推荐到推荐列表 1推荐到首页列表 默认取消推荐]
	 * @return [type]          [description]
	 */
	public static function batchReco($id_arr = [], $action = FALSE )
	{
		//查询最大的order_by
		$max = EwDeco::maximum(['column' => 'order_by']);
		$reco_by  = 0;
		$index_by = 0;
		switch ($action) {
			case 2://推荐到推荐列表
				$reco_by  = 2;
				$index_by = 0;
				$order_by = $max + 1;
				break;

			case 1://推荐到首页列表
				$reco_by  = 0;
				$index_by = 2;
				$order_by = $max + 1;
				break;

			default://取消推荐
				$reco_by  = 0;
				$index_by = 0;
				$order_by = 100;
				break;
		}

		if ($id_arr && is_array($id_arr) && count($id_arr) > 0){
			foreach ($id_arr as $id) {
				$row = EwDeco::findFirst($id);
				$row->reco_by  = $reco_by;
				$row->index_by = $index_by;
				$row->order_by = $order_by;
				$row->save();
			}
		}
	}
}